package com.sqstudio.control
{
	import com.sqstudio.common.DataProxy;
	import com.sqstudio.common.ListCommander;
	import com.sqstudio.common.LunarCalendar;
	import com.sqstudio.common.Sys;
	import com.sqstudio.event.CalendarEvent;
	import com.sqstudio.model.Model;
	import com.sqstudio.view.TopPanel;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;

	/**
	 *头部年月切换控制器 
	 * @author Nestor
	 * 
	 */
	public class TopController
	{
		private var _topPanel:TopPanel;
		private var _yearList:ListCommander;
		private var _monthList:ListCommander;
		public function TopController()
		{
			this._topPanel = new TopPanel();
			Sys.STAGE.addChild(this._topPanel);
			
			var yearDataArr:Array = new Array();
			var yearLabelArr:Array = new Array();
			var yearArr:Array = new Array();
			for(var i:int=1900;i<2101;i++){
				yearDataArr.push(i);
				yearLabelArr.push(i+"年");
			}
			this._yearList = new ListCommander(yearLabelArr,yearDataArr,yearCallBack,new Point(55,200));
			this._topPanel.addChild(this._yearList);
			var monthDataArr:Array = new Array();
			var monthLabelArr:Array = new Array();
			for(var j:int=0;j<12;j++){
				monthDataArr.push(j);
				monthLabelArr.push((j+1)+" 月");
			}
			this._monthList = new ListCommander(monthLabelArr,monthDataArr,monthCallBack,new Point(55,180));
			this._topPanel.addChild(this._monthList);
			this._yearList.y = this._monthList.y = 145;
			this._yearList.x =  40;
			this._monthList.x = 120;
			this._yearList.visible = this._monthList.visible = false;
			
			this._topPanel.leftBtn.addEventListener(MouseEvent.CLICK,leftHandler);
			this._topPanel.rightBtn.addEventListener(MouseEvent.CLICK,rightHandler);
			this._topPanel.todayBtn.addEventListener(MouseEvent.CLICK,todayHandler);
			this._topPanel.yearBtn.addEventListener(MouseEvent.CLICK,yearHandler);
			this._topPanel.monthBtn.addEventListener(MouseEvent.CLICK,monthHandler);
			
			CalendarEvent.Dispatcher.addEventListener(CalendarEvent.DATE_CHANGE,datechangeHandler);
		}
		
		private function yearHandler(event:MouseEvent):void
		{
			this._yearList.visible = !this._yearList.visible;
			this._monthList.visible = false;
			DataProxy.setIndex(this._yearList);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.COUNT_CHANGE,1));
		}
		private function yearCallBack(year:String):void{
			this._yearList.visible = false;
			var tmpDate:Date = Model.CrtDate;
			tmpDate.setFullYear(int(year));
			tmpDate.setDate(1);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.DATE_CHANGE));
			
		}
		
		
		
		private function monthHandler(event:MouseEvent):void
		{
			this._monthList.visible = !this._monthList.visible;
			this._yearList.visible = false;
			DataProxy.setIndex(this._monthList);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.COUNT_CHANGE,1));
		}
		private function monthCallBack(month:String):void{
			this._monthList.visible = false;
			var tmpDate:Date = Model.CrtDate;
			tmpDate.setMonth(int(month));
			tmpDate.setDate(1);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.DATE_CHANGE));
			
		}
		
		
		private function todayHandler(e:MouseEvent):void
		{
			Model.CrtDate = new Date();
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.COUNT_CHANGE,1));
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.DATE_CHANGE));
		}
		
		private function datechangeHandler(event:Event):void
		{
			this._topPanel.updateYearMonth();
		}
		
		private function leftHandler(e:MouseEvent):void
		{
			var tmpDate:Date = Model.CrtDate;
			tmpDate.setMonth(tmpDate.getMonth()-1);
			tmpDate.setDate(1);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.COUNT_CHANGE,1));
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.DATE_CHANGE));
		}		
		private function rightHandler(e:MouseEvent):void
		{
			var tmpDate:Date = Model.CrtDate;
			tmpDate.setMonth(tmpDate.getMonth()+1);
			tmpDate.setDate(1);
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.COUNT_CHANGE,1));
			CalendarEvent.Dispatcher.dispatchEvent(new CalendarEvent(CalendarEvent.DATE_CHANGE));
			
			
		}		
		
	}
}